

package horde.utils;

import horde.internalResources.dictionaries.dictionary;
import java.io.File;
import java.net.InetAddress;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


/**
 * @author Andres Picazo Cuesta
 */
public class Toolbox{
    
    public String [] listDirectoryContents (String directoryName) {
        File file = new File (directoryName);
        if (file != null && file.isDirectory()) return file.list();
        else return null;
    }

    public String getIP () {
        try {
            InetAddress i = InetAddress.getLocalHost();
            return i.getHostAddress();
        }
        catch (Exception failToGetIP) {
            System.err.println ( dictionary.getString("tb.msgFailToGetIP")+"\n" + failToGetIP.getMessage());
            return null;
        }
    }

    public String getHostName () {
        try {
            InetAddress i = InetAddress.getLocalHost();
            return i.getHostName();
        }
        catch (Exception failToGetIP) {
            System.err.println (dictionary.getString("tb.msgFailToGetName")+"\n" + failToGetIP.getMessage());
            return null;
        }
    }

    public boolean isReachable (String ip, int millisToTimeOut) {
        try {
            InetAddress i = InetAddress.getByName(ip);
            return i.isReachable(millisToTimeOut);
        }
        catch (Exception failToGetIP) {
            System.err.println (dictionary.getString("tb.msgFailToGetName")+"\n" + failToGetIP.getMessage());
            return false;
        }
    }

    public String getOS_Name () {
        return System.getProperty("os.name");
    }

    public String getOS_Version () {
        return System.getProperty("os.version");
    }

    public String getFileSeparator () {
        return System.getProperty("file.separator");
    }

    public String getCurrentWorkingDirectory () {
        return System.getProperty("user.dir");
    }
    
    public void println (String text) {
        System.out.println (text);
    }

    public void print (String text) {
        System.out.print(text);
    }

    public String getClassPath () {
        return System.getProperty("java.class.path");
    }

    public String getClassPath (Object obj) {
        java.net.URL url = obj.getClass().getResource(obj.getClass().getSimpleName()+".class");
        return url.toString();
    }

    public Icon getIcon (String iconName, String path) {
        /* Sample path: "../data/icons/" */
        try { return new ImageIcon (getClass().getResource(path + iconName)); }
        catch (Exception e) { System.err.println ("Icon not found at : " + path + iconName +
                "\nException found: " + e.getMessage());
        }
        return null;
    }

    public Icon getIcon (String iconName) {
        return getIcon (iconName, "../data/icons/");
    }

    public Object instanceClass (String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException  {
        return Class.forName(className).newInstance();
    }


    public void test () {
        Toolbox tb = new Toolbox ();
        tb.println("Operative System: " + tb.getOS_Name());
        tb.println("OS Version : " + tb.getOS_Version());
        tb.println("Machine IP : " + tb.getIP());
        tb.println("Machine name: " + tb.getHostName());
        tb.println("Current working directory: " + tb.getCurrentWorkingDirectory());
        tb.println("File separator : " + tb.getFileSeparator());
        tb.println("Classpath: " + tb.getClassPath());
        tb.println("This class path: " + tb.getClassPath(tb));
//        tb.println("Is reachable google.es? " + tb.isReachable("google.es", 1000));
        tb.print("Instancing dummy class: ");
//        try {
//            Dummy dummy = (Dummy) tb.instanceClass("toybox.test.Dummy");
//            tb.println(dummy.sayHi());
//        }
//        catch (Exception dummyFail) {
//            tb.println("Dummy couldn't be created!!\n" + dummyFail.getMessage());
//        }
        tb.println("Contents of this directory: ");
        String [] contents = tb.listDirectoryContents(".");
        if (contents != null) for (String s : contents) tb.println("\t" + s);
        tb.println("Showing icon...");
        String path = tb.getCurrentWorkingDirectory();
//        rute.replace("\\","/");
        path = path.replace("\\", "\\\\");
        path = path + "\\\\data\\\\icons\\\\";
        Icon i = tb.getIcon("close.png", "../data/icons/");
        JOptionPane.showMessageDialog(null, "This is an icon", "Testing...", JOptionPane.INFORMATION_MESSAGE, i);
        
    }
}
